<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/14
 * Time: 12:00
 */

namespace Home\Controller;
use Think\Controller;

class WeiController extends Controller
{


    //初始化方法
    protected function _initialize()
    {
        //检测用户是否登陆 并定义全局变量
        if(isset($_SESSION['msg']['id'])){
            define('UID', $_SESSION['msg']['id']);
        } else {
            $this->redirect('/index.php/Home/Login/login');
        }
    }

   //我的团队
    public function team()
    {
        $id=$_GET['id'];
        $team = M("register")->where("id={$id}")->find();
        //dump($team);die;
        $count1 = M("register")->where("agent_id={$id}")->count();
        //dump($count1);die;
        $count2 = M("register")->where("upper_agent_id={$id}")->count();
        $this->assign("team",$team);


        $this->assign("count1",$count1);
        $this->assign("count2",$count2);
        $this->display("team");
    }

     //下拉刷新
//    public function member()
//    {
//        if(isset($_SESSION['msg']['id'])){
//            $id=$_GET['id'];
//            $data = M("register")->where("agent_id='{$id}' or upper_agent_id='{$id}' or upper_upper_agent_id='{$id}'")->select();
//            $this->display("team");
//        }else
//        {
//            $this->display('Login/login');
//        }
//
//    }

    public function member()
    {
        if(IS_AJAX) {
            $id = $_SESSION['msg']['id'];
            $p = I('p', 1,'intval');
            $field = ['register_name','bind_id', 'headimg', 'create_time','agent_tel'];
            $list = M("register")->where("agent_id='{$id}' or upper_agent_id='{$id}' or upper_upper_agent_id='{$id}'")->field($field)->page($p)->limit(10)->order('create_time asc')->select();
            $this->ajaxReturn(V(1, '团队列表', $list ? $list : []));

        } else {
            $this->display('team');
        }

    }

//    public function down(){
//        if(IS_AJAX){
//            $id=I('id');
//            //echo 123;die;
//            $data = M("register")->where("agent_id='{$id}' or upper_agent_id='{$id}' or upper_upper_agent_id='{$id}'")->order('create_time asc')->select();
//            $this->ajaxReturn($data);
//        }
//    }
//    //上拉加载
//    public function up(){
//        if(IS_AJAX){
//            $id=$_POST['id'];
//            $index = I('index');
//            $size = I('size');
//            //echo json_encode($index);die;
//            $start = ($index-1)*$size;
//            $uer=M('register')->limit($start,$size)->where("agent_id='{$id}' or upper_agent_id='{$id}' or upper_upper_agent_id='{$id}'")->order('create_time desc')->select();
//
//            $su=M('register')->where("agent_id='{$id}' or upper_agent_id='{$id}' or upper_upper_agent_id='{$id}'")->count();
//
//            $data=array(
//                "count"=>$su,
//                'index' => $index,
//                'start' => $start,
//                "info"=>$uer
//            );
//            $this->ajaxReturn($data);
//            //die(json_encode($data,JSON_UNESCAPED_UNICODE));
//
//        }
//    }

     //验证充值权限
    public function check_auth(){
        if(IS_AJAX){
            $id = $_POST['id'];
            $auth = M('register')->where("id = {$id}")->getField('auth');
            $recharge_no = M('register')->where("id = {$id}")->getField('bind_id');
			//$this->ajaxReturn(M('register')->getLastSql());
            if($auth == 1){
                $data=array(
                    'status'=> 1,
                    'recharge_no'=>$recharge_no
                );
            }else{
                $data=array(
                    'status'=> 0
                );
            }
            $this->ajaxReturn($data);
        }
    }


    //进入充值界面
    public function recharge(){
        if($_GET['id']){
            $id = $_GET['id'];
            $recharge_no = $_GET['recharge_no'];
            $money = C('PRODUCT_PRICE');
        }
        $this->assign('money',$money);
        $this->assign('recharge_no',$recharge_no);
        $this->display('recharge');

    }

    //获取前台所需的信息
    public function get_info(){
        if(IS_AJAX){
            $phone = $_POST['phone'];
            $user_info = M('register')->where("tel={$phone}")->find();
            if($user_info){
                $this->ajaxReturn($user_info);
            }
        }
    }

    //我的二级团队
    public function member2()
    {
        $id=$_GET['id'];
        $member2 = M("register")->where("upper_agent_id={$id}")->select();
        $this->assign("member2",$member2);
        $this->display("member2");
    }
 

    public function commission(){
        if (isset($_SESSION['msg']['id'])) {
            if($_GET)//先判断是否通过get传值了
            {
                if(isset($_GET["date"]))//是否存在"id"的参数
                {
                    $id=$_SESSION['msg']['id'];
                    $date=I('date');
					$today = date($date. ' 12:00:01');
                    $yestoday=date('Y-m-d 12:00:00',strtotime("$date -1 days"));
                    $commission = $this->get_day_money($id,$yestoday,$today);//获取昨日工资奖金
//                    $data['time'] = array(array('EGT', $yestoday), array('ELT', $today));
//					if($date!=''){
//                        $team1=M("bonus")->join('register on bonus.mid=register.id')->where('uid='.$id.' and remark=1')->where($data)->select();
//
//                        $team2=M("bonus")->join('register on bonus.mid=register.id')->where('uid='.$id.' and remark=2')->where($data)->select();
//
//                        $team3=M("bonus")->join('register on bonus.mid=register.id')->where('uid='.$id.' and remark=3')->where($data)->select();
//                    }
//					$money1 = 0;
//					foreach ($team1 as $key=> $value) {
//						$money1 += $value['money'];
//					}
//					$money2 = 0;
//					foreach ($team2 as $key=> $value) {
//						$money2 += $value['money'];
//					}
//					$money3 = 0;
//					foreach ($team3 as $key=> $value) {
//						$money3 += $value['money'];
//					}
//
//					$money=$money1+$money2+$money3;
					
					
					
                    $ress=M("register")->where('id='.$id)->find();
                    $commissions=$ress['commissions'];
                    $this->assign("date",$date);
                    $this->assign("commission",$commission);
                    $this->assign("commissions",$commissions);
                    $this->display('commission');
                }else{
                    $id=$_GET['id'];
					$date=date('Y-m-d');
                    $today = date($date. ' 12:00:01');
                    $yestoday=date('Y-m-d',strtotime("$today -1 days"))." 12:00:00";

//					$data['time'] = array(array('EGT', $yestoday), array('ELT', $today));
//					if($date!=''){
//                        $team1=M("bonus")->join('register on bonus.mid=register.id')->where('uid='.$id.' and remark=1')->where($data)->select();
//
//                        $team2=M("bonus")->join('register on bonus.mid=register.id')->where('uid='.$id.' and remark=2')->where($data)->select();
//
//                        $team3=M("bonus")->join('register on bonus.mid=register.id')->where('uid='.$id.' and remark=3')->where($data)->select();
//                    }
//					$money1 = 0;
//					foreach ($team1 as $key=> $value) {
//						$money1 += $value['money'];
//					}
//					$money2 = 0;
//					foreach ($team2 as $key=> $value) {
//						$money2 += $value['money'];
//					}
//					$money3 = 0;
//					foreach ($team3 as $key=> $value) {
//						$money3 += $value['money'];
//					}

//					$money=$money1+$money2+$money3;
                    $commission = $this->get_day_money($id,$yestoday,$today);//获取昨日工资奖金
                    $ress=M("register")->where('id='.$id)->find();
                    $commissions=$ress['commissions'];
                    $this->assign("date",$date);
                    $this->assign("commission",$commission);
                    $this->assign("commissions",$commissions);
                    $this->display('commission');
                }
            }
        }else{
            $this->display('Login/login');
        }
    }

    function get_day_money($id,$beginYesterday,$endYesterday){
        $data['time'] = array('between',array($beginYesterday,$endYesterday));
        $commission = M("bonus")->where('uid='.$id)->where($data)->sum('money');
        return $commission;
    }


    public function getData(){
        if(IS_AJAX){
            $type=$_POST['type'];
            $p=$_POST['p'];
            $date=$_POST['date'];
			$today = date($date. ' 12:00:01');
            $yestoday=date('Y-m-d 12:00:00',strtotime("$date -1 days"));
            //$data['time']=array('like',"$date%");
			$data['time']=array('between',array($yestoday,$today));
            //echo json_encode($data['stime']);die;
            if (!$type||!$date) {
                $this->ajaxReturn(['status'=>0, 'info'=>'参数错误']);
            }
            $id=$_SESSION['msg']['id'];
 
            if($type==1){
                //$list=M("bonus")->join('register on bonus.mid=register.id')->where($data)->field('register_name,mid,time,bind_id,jsmc,stime,sum(money) as money,bonus.tel as phone')->group('mid')->where('uid='.$id.' and remark=1')->page($p)->select();
                $list=M("bonus")->join('register on bonus.mid=register.id')->where($data)->field('register_name,mid,time,bind_id,jsmc,stime,money,bonus.tel as phone')->where('uid='.$id.' and remark=1')->select();
                //echo json_encode(M("bonus")->getLastSql());die;
            }elseif ($type==2) {
                //$list=M("bonus")->join('register on bonus.mid=register.id')->where('uid='.$id.' and remark=2')->page($p)->limit(10)->select();
                $list=M("bonus")->join('register on bonus.mid=register.id')->where($data)->field('register_name,mid,time,bind_id,jsmc,stime,money,bonus.tel as phone')->where('uid='.$id.' and remark=2')->select();
            }elseif ($type==3) {
                //$list=M("bonus")->join('register on bonus.mid=register.id')->where('uid='.$id.' and remark=3')->page($p)->limit(10)->select();
                $list=M("bonus")->join('register on bonus.mid=register.id')->where($data)->field('register_name,mid,time,bind_id,jsmc,stime,money,bonus.tel as phone')->where('uid='.$id.' and remark=3')->select();
            }
            $this->ajaxReturn(['status'=>1, 'info'=>'列表', 'data' =>$list?$list : []]);
                
        }
    }

    //验证身份证号合法性
    public function checkid()
    {
        $certificate = $_POST['certificate'];
        $re = validate_identity($certificate);
        if ($re == null){
            echo "2";
        }
    }
    //二维码
    public function code()
    {
        $register_id=$_GET['register_id'];
        $code=M("code")->where("register_id={$register_id}")->find();

        $this->assign("code",$code);
        $this->display("code");
    }

    /**
     * 生成二维码
     *
     */
    public function qrcode($level = 3, $size = 4)
    {
        if (isset($_SESSION['msg']['id'])) {
            //导入phpqrcode库
            $url= $_SERVER['HTTP_HOST'];//获取主机地址
            $id=I('get.id');
            //若果没有http,则添加
            if (strpos($url, 'http')===false) {
                $url='http://'.$url;
            }
            $url="{$url}/index.php/Home/index/register?id=".$id;
            $code = M("code")->where("register_id='{$id}'")->find();
            if(!$code)
            {
                Vendor('phpqrcode.phpqrcode');
                //保存图片
                $path = './Public/qrcode/';
                if (!file_exists($path)) {
                    mkdir($path, 0700);
                }
                $random = time() . rand(1000, 9999);
                $qrcode = $path . 'qrcode' . $random . '.png';//生成文件名
                $errorCorrectionLevel = intval($level);//容错级别
                $matrixPointSize = intval($size);//生成图片大小
                //生成二维码图片
                ob_clean();
                $object = new \QRcode();
                $object->png($url, $qrcode, $errorCorrectionLevel, $matrixPointSize, 2);

                $qrcode = substr($qrcode,16);
                $param['register_id'] = $id;
                $param['code'] = $qrcode;
                $data = M("code");
                $re = $data->data($param)->add();
                if ($re) {
                    $this->redirect("wei/code/",array('register_id'=>$id));
                } else {
                    $this->redirect("wei/wei");
                }
            }else
            {
                $code=M("code")->where("register_id={$id}")->find();
                $this->assign("code",$code);
                //var_dump($code);die;
                $this->display("code");
            }
        }else{
            $this->display('Login/login');
        }
        
        /*$img = imagecreate(1000, 1000);;
        $bg = imagecolorallocate($img, 255, 255, 255); //设置画布的背景为白色
        header("Content-type: image/png");
        imagepng($img,'./Public/qrcode/qrbg.png');

        imagedestroy($img);*/

    }

    //按时间检索
    public function tongji(){
        if($_GET){
            $id=$_SESSION['msg']['id'];
            $date=I('date');
            //var_dump($date);die;
            //$ress=M("register")->where('id='.$id)->find();
            //$commissions=$ress['commissions'];
            //$commission=$ress['commission'];
            //var_dump($commissions);die;
            $this->assign("date",$date);
            //$this->assign("commission",$commission);
            //$this->assign("commissions",$commissions);
            $this->display('commission');
        }

        $this->display('commission'); 
        
    }

}